package com.hxkj.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * created by kezhijie on 2018/9/6 15:08.
 * email to kezhijie@hxoss.com
 */
public abstract class AES {

    public static String decryptPassword(String passwordBase64) throws Exception {
        String password = new String(Base64.decodeBase64(passwordBase64));
        // 秘钥
        byte[] key = "0123456789ABCDEF".getBytes();
        // 偏移量
        byte[] iv = "0123456789ABCDEF".getBytes();
        // 转为16进制
        byte[] data = Hex.decodeHex(password.toCharArray());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        // 初始化加密类型 秘钥和偏移量
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
        byte[] bytes = cipher.doFinal(data);
        String plainPassword = new String(bytes);
        return plainPassword;

    }

}
